﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using NLBS.Data.Business;
using NLBS.Entity;
using NLBS.Web;
using System.Web;
using NLBS.Resources;
using NLBS.Text;
using NLBS.Validate;
using NLBS.Security;
using NLBS.Data;

namespace NLBS.Views
{
    public interface ICommentFormView
    {
        string username { get; set; }

        string password { get; set; }

        bool register { get; set; }

        string content { get; set; }

        
    }
    public interface ICommentAction<T> : IAction<T>,ICommentFormView
    {
        
    }
    public partial class CommentListPresenter : Presenter<ICommentListView, ICommentAction<Comment>>
    {
        private User currentUser = BlogInfo.Instance().CurrentUser;
        private Right power = BlogInfo.Instance().CurrentUser.Group.Power;
        private Dictionary<string, string> lang = Language.Instance().getLanguage();
        public override void OnViewLoaded()
        {
            if (BlogInfo.Instance().EnableComment == 0)
            {
                return;
            }
            View.EnableComment = true;
            DataList();
        }

        private void DataList()
        {
            List<Comment> lists = CommentData.GetPageComment();
            if (!string.IsNullOrEmpty(View.q))
            {
              List<Comment> searchList=  lists.FindAll(delegate(Comment c)
                {
                    return c.Content.Contains(View.q);
                });
              View.CommentList = searchList;
                return;
            }

            //过滤日志分类评论
            if (View.catID > 0)
            {
                lists = lists.FindAll(delegate(Comment c)
                {
                    return c.Article.CategoryID == View.catID;
                });
                View.pageUrl = "javascript:NLBSPage.CommentCall('{0}','cat'," + View.catID + ")";
            }
            //过滤用户评论
            if (View.userID > 0)
            {
                lists = lists.FindAll(delegate(Comment c)
                  {
                      return c.AuthorID == View.userID;
                  });
                View.pageUrl = "javascript:NLBSPage.CommentCall('{0}','user'," + View.userID + ")";
            }
            //过滤隐藏分类评论

            if (currentUser.Group.Power.View < 2)
            {
                //List<int> catIDList = new List<int>();
                List<NLBS.Entity.Category> catList = CategoryData.GetCategory();

                foreach (NLBS.Entity.Category item in catList)
                {
                    if (item.Hidden)
                    {
                        lists = lists.FindAll(delegate(Comment c)
                        {
                            return c.Article.Mode == 1 && c.Article.CategoryID != item.CategoryID;
                        });
                    }
                }
            }
            View.CommentList = lists;
        }

        public override void OnActionLoaded()
        {
            base.OnActionLoaded();
            
            this.Action.DataSelectSingle += new EventHandler(Action_DataSelectSingle);
            this.Action.DataInsert += new EventHandler(Action_DataInsert);
            this.Action.DataUpdate += new EventHandler(Action_DataUpdate);
            this.Action.DataDelete += new EventHandler(Action_DataDelete);
        }

        void Action_DataDelete(object sender, EventArgs e)
        {
            Comment comm=CommentData.GetCommentByID(this.Action.keyID);
            this.OnDeleteing(new CommentDeleteEventArgs(comm));
            CommentValidate valid = new CommentValidate(NLBS.Action.Delete, this.Action.keyID);

            valid.Validate(comm,
                delegate()
                {
                    CommentData.DeleteCommentByID(this.Action.keyID);
                    //change
                },
           delegate(string error)
           {
               throw new Exception(error);
           });

            this.OnDeleted(new CommentDeleteEventArgs(comm));
        }

        void Action_DataUpdate(object sender, EventArgs e)
        {
            Comment comm = CommentData.GetCommentByID(this.Action.keyID);

            this.OnUpdateing(new CommentUpdateEventArgs(comm));

            CommentValidate valid = new CommentValidate(NLBS.Action.Update, this.Action.keyID);

            valid.Validate(comm,
            delegate()
            {
                StringSecurity.CheckSecurityCode(RequestWrapper.GetFormString("scode"));
                this.Action.Entity.EditMark = BlogInfo.Instance().CurrentUser.UserName + "$|$" + DateTime.Now.ToString();
                this.Action.Entity.IP = RequestWrapper.IP;
                CommentData.UpdateComment(this.Action.Entity);
            },
            delegate(string error)
            {
                throw new Exception(error);
            });

            this.OnUpdated(new CommentUpdateEventArgs(comm));
        }
        
        void Action_DataSelectSingle(object sender, EventArgs e)
        {
            Comment comm= CommentData.GetCommentByID(this.Action.keyID);
            this.OnSelecting(new CommentSelectEventArgs(comm));


            CommentValidate valid = new CommentValidate(NLBS.Action.Update, this.Action.keyID);

            valid.Validate(comm,
            delegate()
            {
                this.Action.Entity = comm;
            },
            delegate(string error)
            {
                throw new Exception(error);
            });
            this.OnSelected(new CommentSelectEventArgs(comm));
        }

        void Action_DataInsert(object sender, EventArgs e)
        {
            //Comment comm = CommentData.GetCommentByID(this.Action.keyID);

            this.OnInserting(new CommentInsertEventArgs(this.Action.Entity));
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                this.Action.Entity.AuthorID = BlogInfo.Instance().CurrentUser.UserID;
            }
            else
            {
                this.Action.Entity.AuthorID = 0;
            }
            
            CommentAddValidate valid = new CommentAddValidate(this.Action,this.Action);
            valid.Validate(
            delegate()
            {
                CommentData.InsertComment(this.Action.Entity);
                //change
            },
            delegate(string error)
            {
                throw new Exception(error);
            });
            this.OnInserted(new CommentInsertEventArgs(this.Action.Entity));
        }
       
    }
}
